package com.bobi.tfca.ui.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.bobi.tfca.R
import com.bobi.tfca.databinding.ItemMemberLeaveBinding
import com.bobi.tfca.databinding.ItemMemberStillBinding
import com.bobi.tfca.network.response.MemberResponse
import com.bobi.tfca.ui.mainnavigation.user.member.MemberLeaveFragment
import com.bobi.tfca.ui.mainnavigation.user.member.MemberStillFragment
import com.bobi.tfca.util.LogUtil
import com.bumptech.glide.Glide

class MemberLeaveAdapter(
    val context: MemberLeaveFragment,
    var memberList: ArrayList<MemberResponse>
) :
    RecyclerView.Adapter<MemberLeaveAdapter.MemberViewHolder>() {

    // 详情头像事件
    private var mOnHeadsClickListener: OnItemClickListener? = null

    // 查询留言事件
    private var mOnLeaveWordsClickListener: OnItemClickListener? = null

    interface OnItemClickListener {
        fun onItemClick(position: Int)
    }

    fun setOnHeadsClickListener(onItemClickListener: OnItemClickListener?) {
        mOnHeadsClickListener = onItemClickListener
    }

    fun setOnLeaveWordsClickListener(onItemClickListener: OnItemClickListener?) {
        mOnLeaveWordsClickListener = onItemClickListener
    }


    fun setItemList(memberList: ArrayList<MemberResponse>) {
        this.memberList = memberList
    }

    inner class MemberViewHolder(private val binding: ItemMemberLeaveBinding) :
        RecyclerView.ViewHolder(binding.root) {

        fun bind(member: MemberResponse, position: Int) {
            val url = member.picture
            if (url.isNotBlank() && (!"null".equals(url))) {
                Glide.with(context).load(url).placeholder(R.drawable.default_avatar)
                    .into(binding.memberHead)
            }
            binding.tvMemberName.text = member.memberName
            binding.tvMemberAge.text = member.memberAge
            binding.tvExitTime.text = member.exitDate
            binding.tvRole.text = member.role
            binding.tvMemberId.text = member.memberId.toString()
            binding.memberHead.setOnClickListener {
                mOnHeadsClickListener?.onItemClick(position)
            }
            binding.tvLeaveWords.setOnClickListener {
                mOnLeaveWordsClickListener?.onItemClick(position)
            }
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MemberViewHolder {
        val itemView =
            ItemMemberLeaveBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        val viewHolder = MemberViewHolder(itemView)
        return viewHolder
    }

    override fun getItemCount(): Int = memberList.size
    override fun onBindViewHolder(holder: MemberViewHolder, position: Int) {
        val member = memberList[position]

        with(holder) {
            bind(member, position)

        }
    }
}

